package lg.minecraft.plugin.worldprotect.config;

import lg.minecraft.plugin.library.config.LGBaseConfig;
import lg.minecraft.plugin.worldprotect.LGWorldProtect;
import lg.minecraft.plugin.worldprotect.WorldSettingType;
import lg.minecraft.plugin.worldprotect.util.WorldSettings;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.World;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public class Worlds extends LGBaseConfig {

    public Worlds() {
        super(LGWorldProtect.getInstance(), "worlds");
        this.load();
    }

    @Override
    public void load() {
        super.load();
        for (String worldName : this.getKeys(false)) {
            World world = Bukkit.getWorld(worldName);
            if (world == null) continue;
            WorldSettings worldSettings = LGWorldProtect.getWorldSettings(worldName);
            worldSettings.disableAll();
            for (String value : this.getStringList(worldName)) {
                WorldSettingType type = WorldSettingType.getValue(value);
                if (type == null) continue;
                worldSettings.enable(type);
            }
        }
    }

    @Override
    public void save() {
        Map<String, WorldSettings> worldSettingsMap = LGWorldProtect.getWorldSettingsMap();
        for (String worldName : worldSettingsMap.keySet()) {
            List<String> list = new ArrayList<>();
            for (WorldSettingType worldSetting : worldSettingsMap.get(worldName).getWorldSettings()) {
                list.add(worldSetting.name());
            }
            this.set(worldName, list);
        }
        super.save();
    }

}
